package tree;

/**题目：合并两颗二叉树
 * @Author Gavin
 * @date 2022.01.10 22:23
 */
public class tree_18 {
    /**
     * 解题思路：递归
     */
    //Time:O(n) Space:O(n)
    public TreeNode solution(TreeNode t1,TreeNode t2){
        if(t1==null&&t2==null)return null;
        if(t1==null)return t2;
        if(t2==null)return t1;
        TreeNode newNode=new TreeNode(t1.val+t2.val);
        newNode.left=solution(t1.left,t2.left);
        newNode.right=solution(t1.right,t2.right);
        return newNode;
    }

    public TreeNode solution_2(TreeNode t1,TreeNode t2){
        if(t1==null&&t2==null)return null;
        if(t1==null)return t2;
        if(t2==null)return t1;
        t1.val+=t2.val;
        t1.left=solution_2(t1.left,t2.left);
        t1.right=solution_2(t1.right,t2.right);
        return t1;
    }
}
